package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 老人家属Service业务层处理
 * 
 * @author ZHD
 * @date 2025-08-20
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WechatService wechatService;
    @Autowired
    private TokenService tokenService;


    /**
     * 微信登录
     * @param userLoginRequestDto 微信登录参数
     * @return 登录信息
     */
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1.调用小程序获取openid
        String openid = wechatService.getOpenid(userLoginRequestDto.getCode());
        //2.根据openid查询老人家属
        FamilyMember member = this.lambdaQuery()
                .eq(FamilyMember::getOpenId, openid)
                .one();
        //3.判断老人家属是否为空
        if(Objects.isNull(member)){
            member = new FamilyMember();
            member.setOpenId(openid);
        }
        //4.调用小程序获取手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        member.setPhone(phone);

        String nickName = Constants.DEFAULT_NICKNAME_PREFIX
                .get(RandomUtil.randomInt(0, Constants.DEFAULT_NICKNAME_PREFIX.size()))
                .concat(phone.substring(phone.length() - 4));
        if(StringUtils.isBlank(member.getName())){
            member.setName(nickName);
        }

        //5.新增或者更新用户
        saveOrUpdateFamilyMember(member);
        //6.分装数据
        Map<String, Object> claims = new HashMap<>();
        claims.put("memberId", member.getId());
        claims.put("nickName", member.getName());
        String token = tokenService.createToken(claims);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        return loginVo;
    }

    /**
     * 新增或者更新老人家属
     * @param member 老人家属
     */
    private void saveOrUpdateFamilyMember(FamilyMember member) {
        if(Objects.isNull(member.getId())){
            //新增
            save(member);
        }else{
            //更新
            updateById(member);
        }

    }

    /**
     * 查询老人家属
     * 
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id)
    {
        return familyMemberMapper.selectById(id);
    }

    /**
     * 查询老人家属列表
     * 
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember)
    {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember)
    {
        return familyMemberMapper.insert(familyMember);
    }

    /**
     * 修改老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember)
    {
        return familyMemberMapper.updateById(familyMember);
    }

    /**
     * 批量删除老人家属
     * 
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids)
    {
        return familyMemberMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人家属信息
     * 
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id)
    {
        return familyMemberMapper.deleteById(id);
    }


}
